#include "StdAfx.h"
#include ".\activeflash.h"
#include "FlashPlayer.h"


using namespace ACTIVECTRL;

class CFlashProxy : public FlashPlayer
{
private:
	IFlashSink* m_pSink;
public:
	CFlashProxy(IFlashSink* pSink):m_pSink(pSink){}
	virtual void FlashCommand(const std::string& command, const std::string& args) 
	{
		if(m_pSink)
			m_pSink->FlashCommand(command,args);
	}
};

CActiveFlash::CActiveFlash(void)
{
	m_pFlashPlayer = NULL;
}

CActiveFlash::~CActiveFlash(void)
{
	if(m_pFlashPlayer)
		delete m_pFlashPlayer;
}
void CActiveFlash::OnNextFrame(DWORD dwElaspe)
{
	if(m_pFlashPlayer)
	{
		m_pFlashPlayer->Update();
		if(m_pFlashPlayer->Render())
		{
			InvalidateRect();
		}
	}
}
BOOL CActiveFlash::LoadMovie(LPCTSTR szMovie)
{
	if(!m_pFlashPlayer)
		m_pFlashPlayer = new CFlashProxy(static_cast<IFlashSink*>(this));
	CRect rc = GetClientRect();
	m_pFlashPlayer->StartAnimation(szMovie,rc.Width(),rc.Height(),NULL);
	m_pFlashPlayer->SetQuality(FlashPlayer::QUALITY_HIGH);
	m_pFlashPlayer->GotoFrame(0);
	StartMovie();
	return TRUE;
}
void CActiveFlash::OnDraw(ICanvas *pCanvas){	if(m_pFlashPlayer)	{		CRect rc = GetClientRect();		ClientToWnd(rc);		DWORD* dwFlashs = (DWORD*)m_pFlashPlayer->GetFlashFrameBuffer();		if(dwFlashs)		{			IBitmap* pBitmap = pCanvas->GetBitmap();			int bytesperline = ((pBitmap->GetWidth()*2+7)&0xFFFFFFF8)>>1;			WORD *pSrc = (WORD*)(*pBitmap)[0];			register BYTE *pData = (BYTE*)dwFlashs;			for(register int line=rc.top; line<rc.bottom; line++)			{				register WPixel *pPixel = (WPixel*)pSrc + line * bytesperline + rc.left;				for(register int col = rc.left; col<rc.right; col++)				{										switch(pData[3])					{					case 0:						break;					case 255:						*(WORD*)pPixel = ((WORD)(pData[2])>>3 << 11 | (WORD)(pData[1]) >> 2 << 5 | (WORD)(pData[0]) >> 3);						break;						/*					default:						{							pPixel->blue=pPixel->blue+((pData[0]>>3)-pPixel->blue)*(pData[3])/(float)255;							pPixel->red=pPixel->red+((pData[2]>>3)-pPixel->red)*(pData[3])/(float)255;							pPixel->green=pPixel->green+((pData[1]>>2)-pPixel->green)*(pData[3])/(float)255;						}						break;*/					}					pPixel++;					pData+=4;				}			}		}	}}
void CActiveFlash::OnSize()
{
	if(m_pFlashPlayer)
	{
		m_pFlashPlayer->MoveWindow(GetClientRect());
	}
}

LRESULT CActiveFlash::WindowProc(UINT message, WPARAM wParam, LPARAM lParam,BOOL& bHandled)
{
	if (((message >= WM_MOUSEFIRST) && (message <= AFX_WM_MOUSELAST)) ||
		((message >= WM_KEYFIRST) && (message <= WM_IME_KEYLAST)) ||
		((message >= WM_IME_SETCONTEXT) && (message <= WM_IME_KEYUP)))
	{
		CRect rc = GetClientRect();
		ClientToWnd(rc);
		CPoint pt(lParam);
		pt.Offset(-rc.TopLeft());
		lParam = MAKELONG(pt.x,pt.y);
		if(message == WM_LBUTTONUP)
		{
			int a =0;
		}
		if(m_pFlashPlayer)
		{
			LRESULT lResult = 0;
			bHandled = m_pFlashPlayer->HandleWindowlessMessage(message, wParam, lParam, &lResult);
			return lResult;
		}
	}
	return __super::WindowProc(message,wParam,lParam,bHandled);
}